using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Runtime.Serialization;
using Unity.VisualScripting;
using UnityEditor;
using UnityEngine;

/*
Value类的编辑器类
*/

[CustomPropertyDrawer(typeof(Value))]
public class ValueEditor: PropertyDrawer {
    
    public static readonly float LINE = 18;
    Value value = null;
    bool showAffect = false;
    float height = LINE;
    Rect anchor;
    
    public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) {
        EditorGUI.BeginProperty(position, label, property);
        if(anchor == null)anchor = position;
        
        Rect tb = EditorGUI.PrefixLabel(position, GUIUtility.GetControlID(FocusType.Passive), label);
        position = tb;

        float affectW = Mathf.Min(50, position.width*0.2f);
        
        Rect valueRect = new Rect(position.x, position.y, (position.width-affectW) / 2, LINE);
        Rect tapRect = new Rect(position.x+valueRect.width, position.y, valueRect.width - 15, LINE);
        Rect affectRect = new Rect(tapRect.x+tapRect.width + 15, position.y, affectW, LINE);
        
        //显示属性值
        
        //反射获取当前属性对象s
        if(value == null){
            object o = WaterCylinder.GetProperty(property);
            value = o!=null?(Value)o:null;
            //value = (Value)property.objectReferenceValue;
        }else{
            /*SerializedProperty p = property.FindPropertyRelative("affects")
                .FindPropertyRelative("Array")
                .FindPropertyRelative("data[0]")
                .FindPropertyRelative("list")
                .FindPropertyRelative("Array")
                .FindPropertyRelative("data[0]")
                .FindPropertyRelative("value");//找到基础层的基础值*/
            EditorGUI.BeginChangeCheck();

            value.value = EditorGUI.FloatField(tapRect, value.Get());

            value.Update();
            EditorGUI.LabelField(valueRect, value.value.ToString());

            if(EditorGUI.EndChangeCheck()){
                EditorUtility.SetDirty(property.serializedObject.targetObject);
            }
            
            showAffect = EditorGUI.BeginFoldoutHeaderGroup(affectRect, showAffect, "Affects");
            if(showAffect){
                List<AffectList> affects = value.GetAffects();
                height = LINE * (affects.Count + 1);

                for(int i = 0; i < affects.Count; i++){
                    Rect r = new Rect(anchor.x + LINE*2, position.y + LINE*(i+1), 300, LINE);
                    AffectList a = affects[i];
                    EditorGUI.LabelField(r, i + ": " + a.ToString());
                }
            }else{
                height = LINE;
            }
            
            EditorGUI.EndFoldoutHeaderGroup();
        }
              
        EditorGUI.EndProperty();
    }

    public override float GetPropertyHeight(SerializedProperty property, GUIContent label) {
        return height;
    }

    public override bool CanCacheInspectorGUI(SerializedProperty property){
        return true;
    }
}
//*/
